﻿@model CategoryModel

@using Telerik.Web.Mvc.UI;
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)
@Html.Telerik().TabStrip().Name("category-edit").Items(x =>
{
    x.Add().Text(T("Admin.Catalog.Categories.Info").Text).Content(TabInfo().ToHtmlString());
    x.Add().Text(T("Admin.Common.SEO").Text).Content(TabSeo().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Categories.Products").Text).Content(TabProducts().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Categories.Discounts").Text).Content(TabDiscounts().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Categories.Acl").Text).Content(TabAcl().ToHtmlString());
    x.Add().Text(T("Admin.Catalog.Categories.Stores").Text).Content(TabStores().ToHtmlString());

    //generate an event
    EngineContext.Current.Resolve<IEventPublisher>().Publish(new AdminTabStripCreated(x, "category-edit"));
}).ClientEvents(events => events.OnSelect("telerik_on_tab_select")).SetSelectedTabIndex(GetSelectedTabIndex())

@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    { 
    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-info-localized",
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Locales[item].Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => Model.Locales[item].Name)
                            @Html.ValidationMessageFor(model => model.Locales[item].Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Locales[item].Description):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Locales[item].Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Locales[item].Description)
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            @Html.HiddenFor(model => model.Locales[item].LanguageId)
                        </td>
                    </tr>
                </table>
                ,
                @<table class="adminContent">
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Name):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(model => model.Name)
                            @Html.ValidationMessageFor(model => model.Name)
                        </td>
                    </tr>
                    <tr>
                        <td class="adminTitle">
                            @Html.NopLabelFor(model => model.Description):
                        </td>
                        <td class="adminData">
                            @Html.EditorFor(x => x.Description, "RichEditor")
                            @Html.ValidationMessageFor(model => model.Description)
                        </td>
                    </tr>
                </table>
            ))
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.CategoryTemplateId):
            </td>
            <td class="adminData">
                @Html.DropDownListFor(model => model.CategoryTemplateId, Model.AvailableCategoryTemplates)
                @Html.ValidationMessageFor(model => model.CategoryTemplateId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PictureId)
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.PictureId)
                @Html.ValidationMessageFor(model => model.PictureId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.ParentCategoryId):
            </td>
            <td class="adminData">
                @(Html.Telerik().ComboBoxFor(x => x.ParentCategoryId).BindTo(Model.ParentCategories).Encode(true)
                            .DataBinding(x => x.Ajax().Enabled(true).Select("AllCategories", "Category", new { selectedId = Model.ParentCategoryId })))
                @Html.ValidationMessageFor(model => model.ParentCategoryId)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PriceRanges):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PriceRanges)
                @Html.ValidationMessageFor(model => model.PriceRanges)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.ShowOnHomePage):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.ShowOnHomePage)
                @Html.ValidationMessageFor(model => model.ShowOnHomePage)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.IncludeInTopMenu):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.IncludeInTopMenu)
                @Html.ValidationMessageFor(model => model.IncludeInTopMenu)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Published):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.Published)
                @Html.ValidationMessageFor(model => model.Published)
            </td>
        </tr>
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.DisplayOrder):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.DisplayOrder)
                @Html.ValidationMessageFor(model => model.DisplayOrder)
            </td>
        </tr>
    </table>
}
@helper TabSeo()
    {
    @(Html.LocalizedEditor<CategoryModel, CategoryLocalizedModel>("category-seo-localized",
               @<table class="adminContent">
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaKeywords):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].MetaKeywords)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaKeywords)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaDescription):
                       </td>
                       <td class="adminData">
                           @Html.TextAreaFor(model => model.Locales[item].MetaDescription)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaDescription)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].MetaTitle):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].MetaTitle)
                           @Html.ValidationMessageFor(model => model.Locales[item].MetaTitle)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.Locales[item].SeName):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(model => model.Locales[item].SeName)
                           @Html.ValidationMessageFor(model => model.Locales[item].SeName)
                       </td>
                   </tr>
                   <tr>
                       <td colspan="2">
                           @Html.HiddenFor(model => model.Locales[item].LanguageId)
                       </td>
                   </tr>
               </table>
               ,
               @<table class="adminContent">
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaKeywords):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.MetaKeywords)
                           @Html.ValidationMessageFor(model => model.MetaKeywords)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaDescription):
                       </td>
                       <td class="adminData">
                           @Html.TextAreaFor(x => x.MetaDescription)
                           @Html.ValidationMessageFor(model => model.MetaDescription)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.MetaTitle):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.MetaTitle)
                           @Html.ValidationMessageFor(model => model.MetaTitle)
                       </td>
                   </tr>
                   <tr>
                       <td class="adminTitle">
                           @Html.NopLabelFor(model => model.SeName):
                       </td>
                       <td class="adminData">
                           @Html.EditorFor(x => x.SeName)
                           @Html.ValidationMessageFor(model => model.SeName)
                       </td>
                   </tr>
               </table>)) 
        
        
<script type="text/javascript">
    $(document).ready(function () {
        $("#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)").click(togglePageSize);
        togglePageSize();
    });

    function togglePageSize() {
        if ($('#@Html.FieldIdFor(model => model.AllowCustomersToSelectPageSize)').is(':checked')) {
            $('#pnlPageSize').hide();
            $('#pnlPageSizeOptions').show();
        } else {
            $('#pnlPageSize').show();
            $('#pnlPageSizeOptions').hide();
        }
    }
</script>

    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AllowCustomersToSelectPageSize):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.AllowCustomersToSelectPageSize)
                @Html.ValidationMessageFor(model => model.AllowCustomersToSelectPageSize)
            </td>
        </tr>
        <tr id="pnlPageSize">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PageSize):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSize)
                @Html.ValidationMessageFor(model => model.PageSize)
            </td>
        </tr>
        <tr id="pnlPageSizeOptions">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.PageSizeOptions):
            </td>
            <td class="adminData">
                @Html.EditorFor(x => x.PageSizeOptions)
                @Html.ValidationMessageFor(model => model.PageSizeOptions)
            </td>
        </tr>
    </table>
}
@helper TabProducts()
    {
    if (Model.Id > 0)
    {
        var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;
            <table class="adminContent">
                <tr>
                    <td>
            @(Html.Telerik().Grid<CategoryModel.CategoryProductModel>()
                .Name("products-grid")
                    .DataKeys(keys =>
                    {
                        keys.Add(x => x.Id);
                    })
                    .DataBinding(dataBinding =>
                    {
                        dataBinding.Ajax()
                            .Select("ProductList", "Category", new { categoryId = Model.Id })
                            .Update("ProductUpdate", "Category")
                            .Delete("ProductDelete", "Category");
                    })
                    .Columns(columns =>
                    {
                        columns.Bound(x => x.ProductName)
                            .ReadOnly();
                        columns.Bound(x => x.IsFeaturedProduct)
                            .Centered();
                        columns.Bound(x => x.DisplayOrder1)
                            .Centered();
                        columns.Bound(x => x.Id)
                            .Template(x => Html.ActionLink(T("Admin.Common.View").Text, "Edit", "Product", new { id = x.ProductId }, new { }))
                            .ClientTemplate("<a href=\"" + @Url.Content("~/Admin/Product/Edit/") + "<#= ProductId #>\">" + T("Admin.Common.View").Text + "</a>")
                            .Title(T("Admin.Common.View").Text)
                            .ReadOnly();
                        columns.Command(commands =>
                        {
                            commands.Edit().Text(T("Admin.Common.Edit").Text);
                            commands.Delete().Text(T("Admin.Common.Delete").Text);
                        })
                        .Width(180);
                    })
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                    .EnableCustomBinding(true))
                    </td>
                </tr>
                <tr>
                    <td width="100%">
                        <input type="submit" id="btnAddNewProduct" name="btnAddNewProduct" value="@T("Admin.Catalog.Categories.Products.AddNew")" onclick="javascript:OpenWindow('@(Url.Action("ProductAddPopup", "Category", new { categoryId = Model.Id, btnId = "btnRefreshProducts", formId = "category-form" }))', 800, 800, true); return false;" class="t-button" />
                        <input type="submit" id="btnRefreshProducts" name="btnRefreshProducts" style="display: none" />
                        <script type="text/javascript">
                            $(document).ready(function () {
                                $('#btnRefreshProducts').click(function () {
                                    //refresh grid
                                    var optionsGrid = $("#products-grid");
                                    optionsGrid.data('tGrid').ajaxRequest();

                                    //return false to don't reload a page
                                    return false;
                                });
                            });
                        </script>
                    </td>
                </tr>
            </table>
    }
    else
    {
            @T("Admin.Catalog.Categories.Products.SaveBeforeEdit")
    }
}

@helper TabDiscounts()
    {
        if (Model.AvailableDiscounts != null && Model.AvailableDiscounts.Count > 0)
        {
            foreach (var discount in Model.AvailableDiscounts)
            {
            <input type="checkbox" name="SelectedDiscountIds" value="@discount.Id"
            @if (Model.SelectedDiscountIds != null && Model.SelectedDiscountIds.Contains(discount.Id))
            {
                <text>checked="checked"</text>
            }
            />@discount.Name
            <br />
            }
        }
        else
        {
            @T("Admin.Catalog.Categories.Discounts.NoDiscounts")
        }
}
@helper TabAcl()
{
    
    <script type="text/javascript">
        $(document).ready(function () {

            $("#@Html.FieldIdFor(model => model.SubjectToAcl)").click(toggleAcl);
        
            toggleAcl();
        });


        function toggleAcl() {
            if ($('#@Html.FieldIdFor(model => model.SubjectToAcl)').is(':checked')) {
                $('#pnl-acl').show();
            }
            else {
                $('#pnl-acl').hide();
            }
        }

    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.SubjectToAcl):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.SubjectToAcl)
                @Html.ValidationMessageFor(model => model.SubjectToAcl)
            </td>
        </tr>
        <tr id="pnl-acl">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AvailableCustomerRoles):
            </td>
            <td class="adminData">
                @if (Model.AvailableCustomerRoles != null && Model.AvailableCustomerRoles.Count > 0)
                {
                    foreach (var customerRole in Model.AvailableCustomerRoles)
                    {
                    <div>
                        <input type="checkbox" name="SelectedCustomerRoleIds" value="@customerRole.Id" checked="@(Model.SelectedCustomerRoleIds != null && Model.SelectedCustomerRoleIds.Contains(customerRole.Id))" />@customerRole.Name
                    </div>
                    }
                }
                else
                {
                    <div>No customer roles defined</div>
                }</td>
        </tr>
    </table>
}

@helper TabStores()
{
    <script type="text/javascript">
        $(document).ready(function () {

            $("#@Html.FieldIdFor(model => model.LimitedToStores)").click(toggleStoreMapping);
        
            toggleStoreMapping();
        });


        function toggleStoreMapping() {
            if ($('#@Html.FieldIdFor(model => model.LimitedToStores)').is(':checked')) {
                $('#pnl-available-stores').show();
            }
            else {
                $('#pnl-available-stores').hide();
            }
        }

    </script>
    <table class="adminContent">
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.LimitedToStores):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.LimitedToStores)
                @Html.ValidationMessageFor(model => model.LimitedToStores)
            </td>
        </tr>
        <tr id="pnl-available-stores">
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.AvailableStores):
            </td>
            <td class="adminData">
                @if (Model.AvailableStores != null && Model.AvailableStores.Count > 0)
                {
                    foreach (var store in Model.AvailableStores)
                    {
                    <div>
                        <input type="checkbox" name="SelectedStoreIds" value="@store.Id" checked="@(Model.SelectedStoreIds != null && Model.SelectedStoreIds.Contains(store.Id))" />@store.Name
                    </div>
                    }
                }
                else
                {
                    <div>No stores defined</div>
                }</td>
        </tr>
    </table>
}
